function concaturl(url) {
  return `${window.RPConfig.mgt}/role/${url}`;
}
function relurl(url) {
  return `${window.RPConfig.baseURL}/sys/role/rel/role/${url}`;
}

// get list
export function queryRole(param) {
  return $http.post(concaturl("list/page"), param).then(res => res.data);
}

// get list
export const getRoleList = param => $http.post(concaturl("list/page"), param);

// 查询角色已分配应用列表
// export function queryAssignedApp({ responseProcess, param }) {
//   $http.post(concaturl("getAssignedAppByRoleId"), param).then(res => {
//     responseProcess(res.data);
//   });
// }
export const queryAssignedApp = param => $http.post(concaturl("getAssignedAppByRoleId"), param);

// queryMenusResourceTree
export const queryMenusResourceTree = param =>
  $http.post(concaturl(`getPermissionTreeByRoleId?roleId=${param.roleId}`));

// GetRoleDetail
export const getRoleDetail = id => $http.post(concaturl("getById?roleId=" + id));

// change lock unlock
export const toggleLockRole = param => $http.post(concaturl(param.status == 0 ? "lock" : "unlock"), param.ids);

// delete by ids
export const deleteByIds = param => $http.post(concaturl("deleteByIds"), param);

// updateById
export const updateRoleById = param => $http.post(concaturl("updateById"), param);

// add role
export const addRole = param => $http.post(concaturl("add"), param);

// saveAssignedResource
export const saveAssignedResource = param => $http.post(concaturl("permission/save"), param);

// 保存角色应用关联
export const saveAssignedApp = param => $http.post(concaturl("roleAppRel/save"), param);

// RoleUserMgQueryUserOfRole
export function roleUserMgQueryUserOfRole({ responseProcess, param }) {
  $http.post(relurl("user/list"), param).then(res => {
    responseProcess(res.data.dataResult);
  });
}

// RoleUserMgQueryOtherUserOfRole
export function roleUserMgQueryOtherUserOfRole({ responseProcess, param }) {
  $http.post(relurl("queryOtherUser"), param).then(res => {
    responseProcess(res.data.dataResult);
  });
}

// add
export const roleUserMgAddUserToRole = param => $http.post(relurl("add"), param);

// delete
export const roleUserMgDeleteUserFromRole = param => $http.post(relurl("delete"), param);

const roleAPI = {
  queryAssignedApiResource: (roleId, appId) => {
    return $http
      .post(`${window.RPConfig.mgt}/role/getApiResourceByRoleId?roleId=${roleId}&appId=${appId}`, {})
      .then(response => response.data);
  },
  saveAssignedApiResource: param => {
    return $http.post(`${window.RPConfig.mgt}/role/permission2/save`, param).then(response => response.data);
  },
  saveRoleApp: param => {
    return $http.post(`${window.RPConfig.mgt}/role/app/update`, param).then(response => response.data);
  },
  queryAssignedMenu: param => {
    return $http.post(`${window.RPConfig.mgt}/role/roleMenu/getMenuByRoleId`, param).then(response => response.data);
  },
  queryUnassignedMenu: param => {
    return $http.post(`${window.RPConfig.mgt}/role/roleMenu/menuListPage`, param).then(response => response.data);
  },
  saveAssignedMenu: param => {
    return $http.post(`${window.RPConfig.mgt}/role/roleMenu/save`, param).then(response => response.data);
  },
  deleteAssignedMenu: param => {
    return $http.post(`${window.RPConfig.mgt}/role/roleMenu/delete`, param).then(response => response.data);
  },
  getRoleByMenu: param => {
    return $http.post(`${window.RPConfig.mgt}/role/roleMenu/getRoleByMenu`, param).then(response => response.data);
  },
  // 根据角色id获取授权的用户
  getListUsersByRoleId: param => {
    return $http.post(`${window.RPConfig.mgt}/role/listUsersByRoleId`, param).then(response => response.data);
  },
  // 根据角色id获取授权用户的组织
  getListOrgByRoleId: param => {
    return $http.get(`${window.RPConfig.mgt}/organization/listOrgByRoleId`, param).then(response => response.data);
  }
};
export default roleAPI;
